package de.itagile.hypersphere
import com.sun.xml.internal.ws.message.RelatesToHeaderclass RelationCreatedByRelationshipsTest extends GroovyTestCase {
	
	def xml = '''
	<ejb-jar id="ejb-jar_ID" version="2.1">
		<enterprise-beans>
			<entity id="Book">
				<ejb-name>Book</ejb-name>
				<local>ejbs.BookLocal</local>
				<prim-key-class>java.lang.Integer</prim-key-class>
				<primkey-field>id</primkey-field>
			</entity>
			<entity id="Page">
				<ejb-name>Page</ejb-name>
				<local>ejbs.PageLocal</local>
				<prim-key-class>java.lang.Integer</prim-key-class>
				<primkey-field>id</primkey-field>
			</entity>
		</enterprise-beans>
		<relationships>
			<ejb-relation>
				<ejb-relation-name>Page-Book</ejb-relation-name>
				<ejb-relationship-role>
					<ejb-relationship-role-name>
						book
					</ejb-relationship-role-name>
					<multiplicity>Many</multiplicity>
					<relationship-role-source>
						<ejb-name>Page</ejb-name>
					</relationship-role-source>
					<cmr-field>
						<cmr-field-name>book</cmr-field-name>
					</cmr-field>
				</ejb-relationship-role>
				<ejb-relationship-role>
					<ejb-relationship-role-name>
						page
					</ejb-relationship-role-name>
					<multiplicity>One</multiplicity>
					<relationship-role-source>
						<ejb-name>Book</ejb-name>
					</relationship-role-source>
					<cmr-field>
						<cmr-field-name>page</cmr-field-name>
						<cmr-field-type>
							java.util.Collection
						</cmr-field-type>
					</cmr-field>
				</ejb-relationship-role>
			</ejb-relation>
			<ejb-relation>
				<ejb-relation-name>Page-Book</ejb-relation-name>
			</ejb-relation>	
		</relationships>
	</ejb-jar>
		'''
	def ejbJar = new EjbJarXmlParser().parseText(xml)
	def ejbTemplate = new EjbTemplate(ejbJar, null, null)
	def relation = ejbTemplate.findRelationsByName("Book")[0]
	
	void testFindsOppositeEntityName() {
		assertEquals("Page", relation.getOppositeEntityName("Book"))
	}
	
	void testGetRelationType() {
		assertEquals("OneToMany", relation.getType("Book"))
		assertEquals("ManyToOne", relation.getType("Page"))
	}
	
	void testGetCollection() {
		assertEquals("java.util.Collection", relation.getCollection("Book"))
		assertEquals("", relation.getCollection("Page"))
	}
	
	void testEquals() {
		def otherRelation = new Relation(ejbJar.relationships.ejbrelation[1])
		assertTrue(relation.equals(relation))
		assertFalse(relation.equals(otherRelation))
	}
	
}